package com.tledu.springboot01.controller;


import com.tledu.springboot01.model.User;
import com.tledu.springboot01.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("user")
@AllArgsConstructor
public class UserController {

    private final IUserService userService;


    @RequestMapping("index")
    public String index(Model model) {
        model.addAttribute("list", userService.list());
        return "user/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public List<User> list() {
        return userService.list();
    }

    @RequestMapping("add")
    @ResponseBody
    public int add(User user) {
        return userService.add(user);
    }


    @RequestMapping("batch-delete")
    @ResponseBody
    public int batchDelete(String ids) {
        List<Integer> idList = Arrays.asList(ids.split(","))
                .stream()
                .map(item -> Integer.parseInt(item))
                .collect(Collectors.toList());
        return userService.batchDelete(idList);
    }
}
